<template>
  <div class="cs">
    <pagehead :rote="rote" />
    <div class="xzbox">
      <el-button type="primary" size="small" :icon="Plus" @click="open(null)"
        >新建</el-button
      >
    </div>
    <el-table :data="tableData.list" style="width: 100%">
      <el-table-column prop="id" label="id" />
      <el-table-column prop="name" label="呢称" />
      <el-table-column prop="permissionName" label="菜单管理" width="500" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button type="primary" @click="open(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paginationbox">
      <el-pagination
        v-model:current-page="paginationData.pageNum"
        :page-size="paginationData.pageSize"
        size="small"
        :background="true"
        layout="total, prev, pager, next"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      v-model="dialogTableVisible"
      title="添加权限"
      width="500"
      :before-close="handleClose"
    >
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item
          label="名称"
          prop="name"
          label-width="100px"
          label-position="left"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请填写权限名称"
          />
        </el-form-item>
        <el-form-item
          label="权限"
          prop="permissions"
          label-width="100px"
          label-position="left"
        >
          <el-tree
            ref="treeRef"
            style="max-width: 600px"
            :data="userGetmenuData"
            show-checkbox
            :default-checked-keys="defaultCheckedKeys"
            node-key="id"
            default-expand-all="true"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="consubmit(formRef)">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { nextTick, onMounted, reactive, ref } from "vue";
import { userGetmenu, userSetmenu, menuList } from "../../../api";
import { Plus } from "@element-plus/icons-vue";
import { useRoute } from "vue-router";
const rote = useRoute();

// imports and composables
const dialogTableVisible = ref(false);
let userGetmenuData = ref([]);
const form = reactive({
  name: "",
  permissions: "",
  id: "",
});
const defaultCheckedKeys = [4, 5];
const treeRef = ref();
const formRef = ref();
let tableData = reactive({
  list: [],
  total: 1,
});

const rules = reactive({
  name: [
    { required: true, message: "请输入你的名称", trigger: "blur" },
    { min: 3, max: 5, message: "名称长度应该3~5", trigger: "blur" },
  ],
});

onMounted(() => {
  userGetmenu().then((data) => {
    userGetmenuData.value = data.data.data;
  });
  getMenuList();
});

const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
});

// 请求获取菜单权限列表数据
const getMenuList = () => {
  menuList(paginationData).then((data) => {
    const { list, total } = data.data.data;
    tableData.list = list;
    tableData.total = total;
  });
};

const handleClose = () => {
  dialogTableVisible.value = false;
  // 重置表单
  formRef.value.resetFields();
  // 重置树状菜单数据
  treeRef.value.setCheckedKeys(defaultCheckedKeys);
};

const consubmit = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys());
      userSetmenu({ name: form.name, permissions, id: form.id }).then(
        (data) => {
          handleClose();
          getMenuList();
        }
      );
    } else {
      console.log(fields, "fields");
      return false;
    }
  });
};

const open = (rowData = {}) => {
  dialogTableVisible.value = true;
  // console.log(rowData, "rodata");
  nextTick(() => {
    if (rowData) {
      Object.assign(form, { id: rowData.id, name: rowData.name });
      treeRef.value.setCheckedKeys(rowData.permission);
    }
  });
};

const handleSizeChange = (val) => {
  paginationData.pageSize = val;
  getMenuList();
};
const handleCurrentChange = (val) => {
  paginationData.pageNum = val;
  getMenuList();
};
</script>

<style lang="less" scoped>
/* styles */
.xzbox {
  padding: 10px 0 10px 10px;
  background: #fff;
}
.paginationbox {
  padding: 10px 0;
  background: #fff;
  .el-pagination {
    padding-right: 50px;
    justify-content: flex-end;
  }
}
</style>